﻿namespace EggOfPanKu.DeckBuilder.Controls
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;

    using EggOfPanKu.Library.Objects;
    using EggOfPanKu.Library.Infrastructure;

    /// <summary>
    /// Interaction logic for CardControl.xaml
    /// </summary>
    public partial class CardControl : UserControl
    {
        #region Fields

        private Card _Card;
        private int _ControlID;

        #endregion Fields

        #region Constructors

        public CardControl()
        {
            InitializeComponent();
        }

        public CardControl(Card cardInput)
        {
            //Initialize visual components of the control
            InitializeComponent();
            //Initialize card and card image
            Card = cardInput;
            InitializeCardImage();
        }

        #endregion Constructors

        #region Delegates

        //Delegate used to bubble Menu Item events up to the page
        public delegate void AddCardHandler(object sender, RoutedEventArgs e);

        //Delegate used to bubble Menu Item Change events up to the page
        public delegate void RemoveCardHandler(object sender, RoutedEventArgs e);

        //Delegate used to bubble Menu Item Change events up to the page
        public delegate void ViewCardHandler(object sender, RoutedEventArgs e);

        public delegate void SideViewCardHandler(object sender, RoutedEventArgs e);

        #endregion Delegates

        #region Events

        //Event used to instantiate the delegates, and call them
        public event AddCardHandler AddCard;
        public event RemoveCardHandler RemoveCard;
        public event ViewCardHandler ViewCard;
        public event SideViewCardHandler SideViewCard;

        #endregion Events

        #region Properties

        public Card Card
        {
            get { return _Card; }
            set { _Card = value; }
        }

        public int ControlID
        {
            get { return _ControlID; }
            set { _ControlID = value; }
        }

        #endregion Properties

        #region Methods

        /// <summary>
        /// Bubbles up the Add Card Event using the corresponding Delegate
        /// </summary>
        /// <param name="sender">control that instigated the event</param>
        /// <param name="e">Arguments passed along with the event</param>
        public void Add_Click(object sender, RoutedEventArgs e)
        {
            AddCard(this, e);
        }

        /// <summary>
        /// Use the Card's ImagePath to create a Bitmap of the card's image
        /// </summary>
        public void InitializeCardImage()
        {
            CardImage.Source = ImageManager.GenerateImageSource(Card.ImagePath);
            CardImage.ToolTip = Card.Name;
        }

        /// <summary>
        /// Bubble up the ViewCard event using the corresponding Delegate
        /// </summary>
        public void PreviewCard()
        {
            ViewCard(this, null);
        }

        /// <summary>
        /// Bubble up the RemoveCard event using the corresponding Delegate
        /// </summary>
        /// <param name="sender">control that instigated the event</param>
        /// <param name="e">Arguments passed along with the event</param>
        private void Remove_Click(object sender, RoutedEventArgs e)
        {
            RemoveCard(this, e);
        }

        /// <summary>
        /// Bubble up the ViewCard event using the corresponding Delegate
        /// </summary>
        /// <param name="sender">control that instigated the event</param>
        /// <param name="e">Arguments passed along with the event</param>
        private void View_Click(object sender, RoutedEventArgs e)
        {
            ViewCard(this, e);
        }

        public void SideView()
        {
            SideViewCard(this, null);
        }

        #endregion Methods
    }
}